﻿Imports TNCDAL

Public Class frmChangePass
    Private _user_id As Integer = -1
    Dim _user As tblUser = Nothing
    Dim uCtrl As New CUserController

    Public Sub New(ByVal _id As Integer)

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        _user_id = _id
    End Sub
    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
    End Sub

    Private Sub frmDoiMatKhau_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load
        _user = uCtrl.SelectRecordByID(_user_id)
        Me.Text = "Đổi mật khẩu: " + _user.UserName
        txtOldPass.Focus()
    End Sub

    Private Sub btDoi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btDoi.Click
        'kiem tra pass co hop le
        If (txtOldPass.Text = "") Then
            lblOldPassError.Visible = True
            txtOldPass.Focus()
            Exit Sub
        End If
        If (txtPassword.Text = "") Then
            lblPassError.Visible = True
            txtPassword.Focus()
            Exit Sub
        End If
        If (txtPassword2.Text = "") Then
            lblPass2Error.Visible = True
            txtPassword2.Focus()
            Exit Sub
        End If
        'kiem tra pass nhap vao co trung hop
        If (txtPassword.Text <> txtPassword2.Text) Then
            MessageBox.Show("2 Password không giống nhau. Nhập lại!")
            txtPassword.Text = ""
            txtPassword2.Text = ""
            lblPass2Error.Visible = False
            lblPassError.Visible = False
            txtPassword.Focus()
            Exit Sub
        End If
        'kiem tra pass co giong hay khong
        'load thong tin mat khau cu
        If (CThanh_Utilities.getMd5Hash(txtOldPass.Text) <> _user.Password) Then
            MessageBox.Show("Mật khẩu cũ nhập không đúng. Nhập lại!")
            txtOldPass.Text = ""
            txtOldPass.Focus()
            Exit Sub
        End If
        _user.Password = CThanh_Utilities.getMd5Hash(txtPassword.Text)
        uCtrl.UpdateRecordWithoutAttach(_user)
        MessageBox.Show("Đã cập nhật mật khẩu thành công!")
        Me.Close()
    End Sub
End Class